﻿using Admin.CYT369.Com.WebBase;
using brand.admin.cyt369.com.services.Models;
using Microsoft.AspNetCore.Mvc;
using System.Buffers.Text;
using Services=brand.admin.cyt369.com.services.Services;

namespace brand.admin.cyt369.com.Areas.BrandAdmin.Controllers
{
    public partial class BrandController
    {
        #region Brand
        /// <summary>
        /// 通过id查询品牌
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetBrandByID(long id)
        {
            var brand = Services.BrandService.Instance.GetBrandByID(id);
            return DefaultJson(brand);
        }

        /// <summary>
        /// 通过审核结果查询品牌
        /// </summary>
        /// <param name="reviewResult"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetBrandByReviewResult()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            string title = Request.Query.GetString("Title");
            var reviewResult = Request.Query.GetInteger("reviewResult");
            var categoryID = Request.Query.GetInteger("CategoryID");
            var isDeleted = Request.Query.GetBoolean("IsDeleted", false);

            var brands = Services.BrandService.Instance.GetBrands(page, rows, ref total,status:reviewResult,name:title, categoryID:categoryID, isDeleted:isDeleted);
            return DefaultJson(new { total = total, rows = brands });
        }

        /// <summary>
        /// 获取品牌扩展信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetBrandExtension(int brandId)
        {
            var brandExtension = Services.BrandService.Instance.GetBrandExtension(brandId);
            return DefaultJson(brandExtension);
        }

        /// <summary>
        /// 获取已经删除的品牌
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDeleteBrands()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            string title = Request.Query.GetString("Title");
            var categoryID = Request.Query.GetInteger("CategoryID");

            var brands = Services.BrandService.Instance.GetBrands(page, rows, ref total, name: title, categoryID: categoryID, isDeleted: true);

            return DefaultJson(brands);
        }


        /// <summary>
        /// 获取品牌审核记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetBrandReviewsByBrandID(int id)
        {
            var items = brand.admin.cyt369.com.services.Services.BrandService.Instance.GetBrandReviewsByBrandID(id);

            return DefaultJson(items);
        }


        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult BrandDelete(int id)
        {
            var retVal = new Admin.CYT369.Com.WebBase.ExecuteResult();
            retVal.Result = Services.BrandService.Instance.BrandDelete(id);
            if (retVal.Result == false)
            {
                retVal.Msg = "删除失败";
            }
            return DefaultJson(retVal);
        }

        /// <summary>
        /// 审核品牌
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DoBrandAudit()
        {
            var reviewRemark = Request.Form.GetString("ReviewRemark");
            var brandId = Request.Form.GetLong("Id", 0);
            var result = Request.Form.GetBoolean("Result", false);
            var reviewerName = this.UserSession.FullName;
            /// 是否审核通过:-2提交审核 -1未通过审核,0未提交审核，1通过审核， 
            if (brand.admin.cyt369.com.services.Services.BrandService.Instance.DoBrandAudit(brandId, (result ? 1 : -1), this.GetUserId(),reviewerName, reviewRemark))
            {
                return Ok();
            }

            return BadRequest("保存审核失败");
        }
        #endregion


        /// <summary>
        /// 获取品牌图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetBrandImagesByBrandID(int id)
        {
            var brandImages = Services.BrandService.Instance.GetBrandImagesByBrandID(id);

            return DefaultJson(brandImages);
        }


        #region BrandType
        public IActionResult CategorysGetByParentID(int id)
        {
            var documentTypes = Services.BrandService.Instance.CategorysGetByParentID(id);

            return DefaultJson(documentTypes);
        }
        #endregion
    }
}
